package tables

import (
	"github.com/GoAdminGroup/go-admin/template/types"

	"github.com/GoAdminGroup/go-admin/modules/db"
	mform "github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
)

// opt01("隐藏", "显示")
// opt01("否", "是")
// opt01("男", "女")
func opt01(s0, s1 string) types.FieldOptions {
	return types.FieldOptions{
		{Value: "1", Text: s1},
		{Value: "0", Text: s0},
	}
}

// filter01("否","是")
// filter01("隐藏", "显示")
func filter01(s0, s1 string) types.FieldFilterFn {
	return func(model types.FieldModel) any {
		if model.Value == "0" {
			return s0
		}
		if model.Value == "1" {
			return s1
		}
		return "unknown"
	}
}

func optsFromTbl(tblName, text, value string) (fopts []types.FieldOption) {
	scodes, _ := db.WithDriverAndConnection("pbootcms", conn).
		Table(tblName).Select(text, value).All()
	fopts = make([]types.FieldOption, len(scodes))
	for i, v := range scodes {
		// fmt.Printf("%+v", v)
		// println()
		// println()
		fopts[i].Text = v[text].(string)
		fopts[i].Value = v[value].(string)
	}
	return
}

var typeOpts = types.FieldOptions{
	{Text: "text", Value: "4"},
	{Text: "uint", Value: "3"},
	{Text: "int", Value: "2"},
	{Text: "float", Value: "1"},
	{Text: "bool", Value: "0"},
}

// 表单提交预处理函数 ,http://doc.go-admin.cn/zh/plugins/admin/
func prefn(field string) types.FormPreProcessFn {
	return func(values mform.Values) mform.Values {
		// cherry editor 里的数据放在 key=code 里
		values[field] = values["code"]
		// fmt.Printf("%+v\n", values)
		return values
	}
}
